//
//  LoginViewController.m
//  SoundPill
//
//  Created by 宋旭东®Des on 15/2/4.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "AFNetworking.h"
#import "PhoneRegisterViewController.h"
#import "PhoneLoginViewController.h"
#import "ResetPasswordViewController.h"
#import <CoreLocation/CoreLocation.h>
#define Screen_height [[UIScreen mainScreen] bounds].size.height
#define Screen_width  [[UIScreen mainScreen] bounds].size.width
#define IOS8 [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0
@interface LoginViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
}
@property (strong, nonatomic) UIImageView *backgroundImageView;
@property (strong, nonatomic) NSMutableDictionary *userInfo;
@property (strong, nonatomic) NSUserDefaults *user;
@property (strong, nonatomic) CALayer *layer;

@end

@implementation LoginViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        //将数据(uid和token)从本地清空
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:@"uid"];
        [defaults removeObjectForKey:@"token"];
        
        [defaults removeObjectForKey:@"username"];
        [defaults removeObjectForKey:@"sex"];
        [defaults removeObjectForKey:@"location"];
        
        [defaults synchronize];
        //删除缓存头像
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *headImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"selfPhoto.jpg"];
        [fileManager removeItemAtPath:headImageFilePath error:nil];
        
        self.userInfo = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNeedsStatusBarAppearanceUpdate];
    
    self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, Screen_height)];
    self.backgroundImageView.image = [UIImage imageNamed:@"loginbackground"];
    [self.view addSubview:self.backgroundImageView];
    
//    UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.22 * Screen_width + 10, 0.27 * Screen_height, 0.5625 * Screen_width, 0.317 * Screen_height)];
//    //logoImageView.image = [UIImage imageNamed:@"bigLogo.png"];
//    [self.backgroundImageView addSubview:logoImageView];
    [self createSkipButton];
    [self registerAndLogin];
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(skip:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self.view addGestureRecognizer:recognizer];

    _locationManager=[[CLLocationManager alloc]init];
    if ([CLLocationManager locationServicesEnabled]) {
//        NSLog(@"定位服务当前可能尚未打开，请设置打开！");
        return;
    }
    //如果没有授权则请求用户授权
    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        if (IOS8) {
            [_locationManager requestWhenInUseAuthorization];
        }
    }

}

#pragma mark - 先逛逛按钮
- (void)createSkipButton
{
    // 先逛逛
    UIButton *skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [skipButton setFrame:CGRectMake(Screen_width - 75, 25, 70, 15)];
    [skipButton setImage:[UIImage imageNamed:@"login_skip"] forState:UIControlStateNormal];
//    [skipButton setTitle:@"跳过" forState:UIControlStateNormal];
//    skipButton.layer.cornerRadius = 30;
//    skipButton.layer.masksToBounds = YES;
//    skipButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
    skipButton.alpha = 1;
    [skipButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [skipButton addTarget:self action:@selector(skip:) forControlEvents:UIControlEventTouchUpInside];
    [skipButton setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:skipButton];
}

- (void)skip:(id)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.8f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromTop;
    transition.delegate = self;
    [self.view.superview.layer addAnimation:transition forKey:nil];
    [self.view removeFromSuperview];
    [[AppDelegate shareDelegate] chooseTabBarController];
}

#pragma mark 创建登录和注册按钮
- (void)registerAndLogin
{
    // 登录按钮
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginButton setFrame:CGRectMake(0, Screen_height - 0.07 * Screen_height, Screen_width / 2, 0.07 * Screen_height)];
    [loginButton setBackgroundColor:[UIColor blackColor]];
    [loginButton setTitle:@"登 录" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    // 注册按钮
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerButton setFrame:CGRectMake(Screen_width / 2, Screen_height - 0.07 * Screen_height, Screen_width / 2, 0.07 * Screen_height)];
    [registerButton setBackgroundColor:[UIColor whiteColor]];
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    registerButton.layer.borderColor = [[UIColor blackColor] CGColor];
    registerButton.layer.borderWidth = 4;
    [registerButton addTarget:self action:@selector(registerButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];
    
}

- (void)loginButton:(UIButton *)button
{
    PhoneLoginViewController *phoneLoginVC = [[PhoneLoginViewController alloc] init];
    [self presentViewController:phoneLoginVC animated:YES completion:nil];
}

- (void)registerButton:(UIButton *)button
{
    PhoneRegisterViewController *phoneRegisterVC = [[PhoneRegisterViewController alloc] init];
    [self presentViewController:phoneRegisterVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
